﻿using System;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;

namespace hello_accinv_New_
{
    public partial class InventoryForm : Form
    {
        private Database db;

        public InventoryForm()
        {
            InitializeComponent();

            db = new Database();

            LoadProduct();
            LoadCategoryList();

            dataGridView.ClearSelection();
            dataGridView.ClearSelection();
        }

        private void LoadProduct()
        {
            dataGridView.DataSource = db.Select("P.PCode, P.Name AS PName, P.UnitPrice, P.Quantity, P.Detail, P.Remark, C.Name AS CName, C.Unit", "Product P INNER JOIN Category C ON P.CategoryID = C.CategoryID", "P.Flag = '1'");
            dataGridView.ClearSelection();
        }

        private void LoadProduct(string category)
        {
            dataGridView.DataSource = db.Select("P.PCode, P.Name AS PName, P.UnitPrice, P.Quantity, P.Detail, P.Remark, C.Name AS CName, C.Unit", "Product P INNER JOIN Category C ON P.CategoryID = C.CategoryID", "P.Flag = '1' AND C.Name = '" + category + "'");
            dataGridView.ClearSelection();
        }

        private void LoadCategoryList()
        {
            DataTable dt = db.Select("Name", "Category", "1 ORDER BY Name ASC");
            List<string> lst = new List<string>();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lst.Add(dt.Rows[i]["Name"].ToString());
            }
            cmbCategoryList.DataSource = lst;
            cmbCategoryList.SelectedIndex = -1;
            dt.Dispose();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void btnAddNewProduct_Click(object sender, EventArgs e)
        {
            AddProductForm addproduct = new AddProductForm();
            addproduct.ShowDialog();
            addproduct.Dispose();
        }

        private void btnManageCategory_Click(object sender, EventArgs e)
        {
            ManageCategoryForm category = new ManageCategoryForm();
            category.ShowDialog();
            category.Dispose();
        }

        private void btnEditProduct_Click(object sender, EventArgs e)
        {
            EditProductForm editproduct = new EditProductForm();
            editproduct.ShowDialog();
            editproduct.Dispose();
        }

        private void btnAddProductToStock_Click(object sender, EventArgs e)
        {
            AddStockForm addstock = new AddStockForm();
            addstock.ShowDialog();
            addstock.Dispose();
        }

        private void dataGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            if (dataGridView.RowCount > 0)
            {
                ReRunNumber();

                if (dataGridView.SelectedRows.Count > 0)
                {
                    btnEditProduct.Enabled = true;
                    btnDeleteProduct.Enabled = true;
                }
                else
                {
                    btnEditProduct.Enabled = false;
                    btnDeleteProduct.Enabled = false;
                }
            }
        }

        private void ReRunNumber()
        {
            for (int i = 1; i <= dataGridView.RowCount; i++)
            {
                dataGridView[0, (i - 1)].Value = i;
            }
        }

        private void radioFilterCategory_CheckedChanged(object sender, EventArgs e)
        {
            if (radioFilterCategory.Checked)
            {
                cmbCategoryList.Enabled = true;
            }
        }

        private void radioShowAll_CheckedChanged(object sender, EventArgs e)
        {
            if (radioShowAll.Checked)
            {
                cmbCategoryList.SelectedIndex = -1;
                cmbCategoryList.Enabled = false;

                LoadProduct();
            }
        }

        private void cmbCategoryList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radioFilterCategory.Checked)
            {
                LoadProduct(cmbCategoryList.SelectedItem.ToString());
            }
        }

        private void btnDeleteProduct_Click(object sender, EventArgs e)
        {
            if (dataGridView.RowCount > 0)
            {
                if (MessageBox.Show("Code\t: " + dataGridView.SelectedRows[0].Cells[1].Value + "\nName\t: " + dataGridView.SelectedRows[0].Cells[2].Value + "\nCategory\t: " + dataGridView.SelectedRows[0].Cells[8].Value + "\n\nDo you want to delete this product?",
                    "Confirmation",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Exclamation) == DialogResult.Yes)
                {
                    db.Update("Product", "Flag = 0", "PCode = '" + dataGridView.SelectedRows[0].Cells[1].Value + "'");
                    db.Insert("ActionLog", "'" + CustomDateTime.GetCurrentDate() + "','" + CustomDateTime.GetCurrentTime() + "','" + GlobalClass.fullName + " deleted product " + dataGridView.SelectedRows[0].Cells["PName"].Value + ".'");
                }
            }
            dataGridView.ClearSelection();
        }
    }
}
